package com.prince.aspect;


import com.prince.util.JsonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 犬小哈 （微信号：小哈学Java）
 * @site www.exception.site
 * @date 2019/2/12
 * @time 下午9:19
 * @discription
 **/
@Aspect
@Component
//@Profile({"dev", "test"}) //这个注解打开以后就不显示后台打印日志了
public class AppLogAspect {

    private final static Logger logger = LoggerFactory.getLogger(AppLogAspect.class);

    /**
     * 以自定义 @WebLog 注解为切点
     */
    @Pointcut("@annotation(com.prince.aspect.AppLog)")
    public void appLog() {
    }

    /**
     * 在切点之前织入
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Before("appLog()")
    public void doBefore(JoinPoint joinPoint){
        // 开始打印请求日志
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
         //打印请求相关参数
        logger.info("============= Start ================");
         //打印请求 url
//        logger.info("URL    : {}", request.getRequestURL().toString() );
         //打印描述信息
        // 打印调用 controller 的全路径以及执行方法
//        logger.info("请求方法    : {}类的{}()方法", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
         //打印 Http method
//        logger.info("请求类型    : {}", request.getMethod());
        // 打印请求地址
//        logger.info("请求地址    : {}" , request.getRequestURI());
         //打印请求入参
        String clazz = joinPoint.getSignature().getDeclaringTypeName();
        clazz = clazz.substring(clazz.lastIndexOf(".")+1)+".";
        logger.info(request.getMethod() + "方式  " + clazz + joinPoint.getSignature().getName()+"() 参数:"+ JsonUtil.toJSONString(request.getParameterMap()));
    }

    /**
     * 在切点之后织入
     *
     * @throws Throwable
     */
    @After("appLog()")
    public void doAfter() {
        // 接口结束后换行，方便分割查看
        logger.info("============== End ================="  );
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        logger.error(request.getMethod() + "方式  " +request.getRequestURI() + "  参数:"+JSONUtil.parseObj(request.getParameterMap()));

    }

    /**
     * 功能描述: App异常打印请求数据
     *
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/5/20 10:31
     */
    @AfterThrowing("appLog()")
    public void afterThrowing(JoinPoint joinPoint) {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String clazz = joinPoint.getSignature().getDeclaringTypeName();
//        clazz = clazz.substring(clazz.lastIndexOf(".") + 1) + ".";
//        logger.error(request.getMethod() + "方式  " + clazz + joinPoint.getSignature().getName() + "() 参数:" + JSONUtil.parseObj(request.getParameterMap()));
    }



}
